package com.zohnblog.service.impl;

import com.zohnblog.dao.RegisterMapper;
import com.zohnblog.model.domain.User;
import com.zohnblog.service.IRegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Transactional
public class RegisterServiceImpl implements IRegisterService {
    @Autowired
    private RegisterMapper registerMapper;
    //  密码需要设置编码器
    BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
    // 注册用户权限id
    final static Integer authorityId = 2;

    // 注册
    @Override
    public void register(User user) {
        user.setPassword(encoder.encode(user.getPassword()));
        user.setCreated(new Date());
        user.setValid(1);
        registerMapper.register(user);
        registerMapper.addAuthority(user.getId(), authorityId);
    }


    // 查看用户名是否已注册
    public boolean sameUsername(String username){
        if(registerMapper.sameUsername(username) > 0){
            return true;
        }else {
            return false;
        }
    }
}
